
package com.shop.cloud.mall.api.controller;

import cn.hutool.core.util.StrUtil;
import com.shop.cloud.common.core.util.R;
import com.shop.cloud.common.portal.annotation.ApiLogin;
import com.shop.cloud.common.portal.util.ApiUtil;
import com.shop.cloud.common.portal.util.ThirdSessionHolder;
import com.shop.cloud.common.portal.util.UserOnlineUtil;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import jakarta.servlet.http.HttpServletRequest;

/**
 * 商城用户在线统计
 *
 * @author
 * @date 2022-06-04 11:09:55
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/useronline")
@Tag(description = "useronline", name = "商城用户在线统计API")
public class UserOnlineController {

    private final UserOnlineUtil userOnlineUtil;

	/**
	 * 接收商城用户心跳
	 * @return R
	 */
	@Operation(summary = "接收商城用户心跳")
    @GetMapping("/heartbeat")
	@ApiLogin
    public R getHeartBeat(HttpServletRequest request) {
		//获取header中的客户端类型
		String clientTypeHeader = ApiUtil.getClientType(request);
		String userId = ThirdSessionHolder.getUserId();
		if(StrUtil.isNotBlank(clientTypeHeader) && StrUtil.isNotBlank(userId)){
			userOnlineUtil.online(clientTypeHeader, userId);
			return R.ok();
		}else{
			return R.failed();
		}
    }

}
